/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Description: hkrr head functions
 * Author: wangzhu
 * Create: 2021-07-05
 * Notes: it includes header file for hkrr.c and hkrr_utils.c
 */

#ifndef HKRR_KERNEL_H
#define HKRR_KERNEL_H

#include <linux/kallsyms.h>
#include <linux/sort.h>
#include <linux/libfdt.h>
#include <linux/reboot.h>

#ifdef CONFIG_ARM64
#include <asm/kernel-pgtable.h>
#include <asm/debug-monitors.h>
#else
#include <asm/fixmap.h>
#include <asm/io.h>
#endif

#define OBJTAB_SIZE 12
#define KAS_OWNER_TAB_SIZE 160
#define KB_SHIFT 10

#define HKRR_ERROR 1
#define HKRR_OK    0

#define HKRR_MAX_BASE 16
#define HKRR_MIN_BASE 2

#ifdef CONFIG_ARM64

#ifdef CONFIG_CPU_BIG_ENDIAN
#define ELF_MAGIC 0x7f454c4602020100UL
#else
#define ELF_MAGIC 0x00010102464c457fUL
#endif

#else /* !CONFIG_ARM64 */

#ifdef CONFIG_CPU_BIG_ENDIAN
#define ELF_MAGIC 0x7f454c4601020100UL
#else
#define ELF_MAGIC 0x00010101464c457fUL
#endif

#define HIGH_BIT_MASK 0x80000000

#endif

#define LLU	long long unsigned
#define LLD	long long signed

#define R_AARCH64_ADR_GOT_PAGE        311
#define R_AARCH64_LD64_GOT_LO12_NC    312

#define R_ARM_REL32		3
#define R_ARM_RELATIVE		23
#define R_ARM_BASE_PREL		25
#define R_ARM_GOT_BREL		26
#define R_ARM_MOVW_PREL_NC	45
#define R_ARM_MOVT_PREL		46

struct hkrr_kastab {
	int kas_offset;
	unsigned int kas_name;
};

#ifdef CONFIG_CORTEX_A9

#define MAX_REG_COUNTER_OFFS_CNT 5
struct random_source {
	const char *dts_compatible;
	unsigned int reg_conter_offs[MAX_REG_COUNTER_OFFS_CNT];
	int offs_cnt;
};
#endif

#if CONFIG_RTOS_HKRR_DATA_SIZE == 0
#error "dynamic memory is not support by kernel"
#endif

void __init hkrr_log_str(const char *str);
void __init hkrr_log_strnum(const char *str, unsigned long num, bool dec);
void __init hkrr_print_log(void);
__init uint64_t hkrr_get_hkrr_seed(void);
void __init hkrr_print_addrs(void);

#ifdef CONFIG_ARM64
extern phys_addr_t __fdt_pointer __initdata;
#endif

extern char __hkrr_table_start[], __hkrr_table_end[];
extern char __text_text_start[], __text_text_end[], __text_text_end2[];
extern char __got_start[], __got_end[];
extern char __hkrr_data_start[], __hkrr_data_end[];
extern char __hkrr_text_start[], __hkrr_text_end[];
extern char __hkrr_objtab_start[], __hkrr_objtab_end[];
extern char __hkrr_kasownertab_start[], __hkrr_kasownertab_end[];

#ifdef CONFIG_RTOS_EXTRACT_KALLSYMS
extern void hkrr_kallsyms_data_setup(u8 *kas_names, struct hkrr_kastab *kas);
/* must match to include/linux/rtos/extract_kallsyms.h */
extern unsigned int kallsyms_num_syms __weak;
extern int *kallsyms_offsets __weak;
extern u8 *kallsyms_names __weak;
extern unsigned int *kallsyms_markers __weak;
#else
/* NB!! must match to  kernel/kallsyms.c */
extern int kallsyms_offsets[] __weak;
extern uint8_t kallsyms_names[] __weak;
extern unsigned int kallsyms_markers[] __weak;
extern const unsigned int kallsyms_num_syms __weak;
#endif

#ifdef CONFIG_ARM_UNWIND
#include <asm/unwind.h>
struct unwind_idx2 {
	unsigned long addr;
	unsigned long insn;
};

extern struct unwind_idx __start_unwind_idx[];
extern const struct unwind_idx __stop_unwind_idx[];
extern const struct unwind_idx *__stop_unwind_idx2;
#endif

#ifdef CONFIG_ARM64
extern void flush_cache_all(void);
#define HKRR_FLUSH_CACHE_ALL flush_cache_all
#else
extern void v7_flush_kern_cache_all(void);
#define HKRR_FLUSH_CACHE_ALL v7_flush_kern_cache_all
#endif

#ifndef CONFIG_ARM64
extern unsigned long __atags_pointer;
#endif

#ifdef CONFIG_DEBUG_LL
extern void printascii(const char *s);
#endif

#endif
